package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

/**
 * createTime：2021/8/23 9:48
 * description：Baked 等待原料全部准备好进行使用(这里是锅中加热)
 */
public class Baked {
    static class Pan {
    }

    static Pan pan(Batter b) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage());
        }
        return new Pan();
    }

    static Baked heat(Pan p) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage());
        }
        return new Baked();
    }

    static CompletableFuture<Baked> bake(CompletableFuture<Batter> cfb) {
        return cfb.thenApplyAsync(Baked::pan)
                .thenApplyAsync(Baked::heat);
    }

    public static Stream<CompletableFuture<Baked>> batch() {
        CompletableFuture<Batter> batter = Batter.mix();

        return Stream.of(bake(batter),
                bake(batter),
                bake(batter),
                bake(batter)
        );

    }

}
